Component({
  data: {
    dateArrayShow: ["今天", "明天"], // 日期数组
    dateArray: [], // 日期数组
    timeArray: [], // 时间数组
    value: [0, 0], // 当前选择的日期和时间
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {
      this.initDateArray();
      this.initTimeArray(0);
    },
  },

  methods: {
    // 初始化日期数组
    initDateArray: function () {
      let dateArray = [];
      let now = new Date();
      let tomorrow = new Date(now.getTime() + 24 * 60 * 60 * 1000);
      dateArray.push(this.formatDate(now));
      dateArray.push(this.formatDate(tomorrow));
      this.setData({
        dateArray: dateArray,
      });
    },
    // 初始化时间数组
    initTimeArray: function (type) {
      console.log(111);
      let timeArray = [];
      let now = new Date();
      if (type == 1) {
        now.setHours(9, 0, 0)
      }
      let startHour = now.getHours();
      // console.log(startHour);
      let startMinute = Math.ceil(now.getMinutes() / 40) * 40;
      if (startMinute == 60) {
        startHour += 1;
        startMinute = 0;
      }
      let endHour = 22;
      let endMinute = 0;
      for (let i = startHour; i <= endHour; i++) {
        let hourStr = i < 10 ? '0' + i : '' + i;
        for (let j = startMinute; j <= 30; j += 30) {
          let minuteStr = j == 0 ? '00' : '' + j;
          timeArray.push(hourStr + ':' + minuteStr);
        }
        startMinute = 0;
      }
      this.setData({
        timeArray: timeArray,
      });
    },
    // 格式化日期
    formatDate: function (date) {
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      return year + '-' + (month < 10 ? '0' + month : '' + month) + '-' + (day < 10 ? '0' + day : '' + day);
    },
    onDateArray(e) {
      if (e.currentTarget.dataset.index == 0) {
        this.initTimeArray(0);
      } else {
        this.initTimeArray(1);
      }

    },
    // picker-view改变事件
    bindPickerChange: function (e) {
      this.setData({
        value: e.detail.value,
      });
    },
  },
});